package com.googlecode.perfectable.eventable.routing;

import static com.google.common.base.Preconditions.checkNotNull;

public class Router<E> implements Sink<E> {
	private Sink<E> target = new Buffer<>();

	protected Router() {
	}

	public static <X> Router<X> create() {
		return new Router<>();
	}

	@Override
	public void receive(E message) {
		this.target.receive(message);
	}

	public void route(Sink<E> newTarget) {
		checkNotNull(newTarget);
		if(this.target instanceof Buffer) {
			((Buffer<E>) this.target).flush(newTarget);
		}
		this.target = newTarget;
	}

}
